<template>
    <div class="app-container">
    <el-upload style=""
                     class="upload-demo"
                     ref="upload"
                     :limit="fileLimit"
                     drag
                     action="#"
                     :http-request="httpRequest"
                     :on-preview="handlePreview"
                     :on-remove="handleRemove"
                     :on-exceed="handleExceed"
                     :before-remove="beforeRemove"
                     :before-upload="beforeUpload"
                     :on-change="handleChange"
                     :auto-upload="false"
                     :on-success = "onSuccess"
                     accept=".zip"
                     :multiple="true">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">最多{{this.fileLimit}}个附件，每个附件不超过{{this.fileSizeLimitByMb}}MB,当前选择文件数：{{ this.uploadCount }}</div>
            <!-- <el-progress style="width: 200px;margin-top: 8px"  :percentage="progressPercent" /> -->
          </el-upload>
          <div slot="footer" class="dialog-footer">
            <!-- <el-button @click="dialogFormVisible = false">取 消</el-button> -->
            <el-button type="primary" @click="uploadAll">开始上传</el-button>
          </div>
        </div>
   </template>
<script>
	import {
		Decrypt
	} from "@/utils/keyData";
	import JSZip from "jszip";
  
  

export default {
    name: "SelfUploadMultip",
    data() {
        return {
            progressPercent: 0,
            fileLimit: 50,
            fileSizeLimitByMb:2,
            fileLists:[],
            uploadCount:0,
            dataJson: {
			jsonStr: []
				},
        };
    },

    methods: {
        onSuccess(response, file, fileList){
            console.log("1111111111111111111111111111111111")
            this.$refs.upload.clearFiles();
        },
        handleChange(file, fileList){
          this.fileLists =fileList;
          this.uploadCount=fileList.length;
        },
      
        sqlByParam(sql){

          this.$electron.ipcRenderer.send('run-database',sql , []);
          this.$electron.ipcRenderer.on('run-reply', (event, res) => {
          return res;
       });

        },
     
       

        uploadAll(){
            if(this.fileLists ==0){
            this.$message.warning(
				"请选择上传数据，在开始上传"
			);
            return;
          }

          this.$refs.upload.submit();
        //   if(this.fileLists>this.fileLimit){
        //     this.$message.warning(
        //  "当前限制选择"+ this.fileLimit+"个文件，本次选择了 "+fileList.length+"个文件，请重新选择"
        // );
        //   }
        // this.fileLists.forEach((file) => {
        //     console.log(file)
        //    this.httpRequest(file);
        //   });
        },
        httpRequestlao(file) {

        },
        httpRequest(file) {
            console.log(file)
				var new_zip = new JSZip();
				new_zip.loadAsync(file.file)
					.then(() => {
						new_zip
							.file("data.json")
							.async("string")
							.then((content) => {
              
								this.dataJson.jsonStr = JSON.parse(Decrypt(content));
                console.log(	this.dataJson.jsonStr)

                //判断轮次本地是否有 如果有删除 没有新增
               var sql1="insert into sys_dept (dept_id) VALUES (12)";

//                 this.$electron.ipcRenderer.send('run-database',sql1 , []);

//                 this.$electron.ipcRenderer.on('run-reply', (event, res) => {
//     console.log(res);

// });


   

								//   this.dataJson.jsonStr=Decrypt(content);
								// importData(this.dataJson).then(response => {
								// 	console.log(response);
								// 	if (response.code == 200) {
                //                         this.$modal.msgSuccess(file.file.name+"导入成功")
                //                         this.$refs.upload.clearFiles();
								// 		// this.$modal.msgSuccess("导入成功，本次导入数据共" + response.data.allNum +
								// 		// 	"条，成功：" + response.data.succNum + "条，失败：" + response.data
								// 		// 	.errNum + "条");
								// 		// this.getList();
								// 	} else {
								// 		this.$message.error(
								// 			"导入失败，数据格式有误，请联系管理员"
								// 		);
								// 	}
								// 	// this.$modal.msgSuccess("后台正在执行导入，请稍后刷新页面");

								// });

							});
					})
					.catch((err) => {
						console.log(file)
            // console.log(err.file.name)
						// this.$message.error(
						// 	"此数据包数据规则已不符合导入规则，请重新选择11。"
						// );
            this.$confirm('此数据包数据规则已不符合导入规则请记录文件名：'+file.file.name, '提示', {
  confirmButtonText: '确定',
  cancelButtonText: '取消',
  showCancelButton: false,
  type: 'warning'
})
  .then(() => {
    // 用户点击了确定按钮，执行相应的操作
    // 可以在这里编写相关的逻辑代码
  });

					});

				//此数据包数据规则已不符合本次信息采集规则，请点击“首次填报”进行填报。
			},

    },

}
</script>
